package com.zhang.common.core.handle;

import com.zhang.common.core.constant.ResultConstant;
import com.zhang.common.core.exception.BaseException;
import com.zhang.common.core.exception.InterfaceException;
import com.zhang.common.core.exception.CheckedException;
import com.zhang.common.core.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 统一异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(InterfaceException.class)
    public Result interfaceExceptionHandler(HttpServletResponse response, InterfaceException ex) {
        log.error(ex.getMessage(),ex);
        return new Result(ex.getCode(), ex.getMessage());
    }


//    @ResponseStatus(HttpStatus.OK)
//    @ExceptionHandler(UserNotFoundException.class)
//    public Result userNotFoundExceptionHandler(HttpServletResponse response, UserNotFoundException ex) {
//        log.error(ex.getMessage(),ex);
//        return new Result(ex.getCode(), ex.getMessage());
//    }

//    @ResponseStatus(HttpStatus.OK)
//    @ExceptionHandler(ParamNullException.class)
//    public Result paramNullExceptionHandler(HttpServletResponse response, ParamNullException ex) {
//        log.error("异常信息【{}】",ex.getMessage(),ex);
//        return new Result(ex.getCode(), ex.getMessage());
//    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(CheckedException.class)
    public Result checkedExceptionExceptionHandler(HttpServletResponse response, CheckedException ex) {
        log.error("异常信息【{}】",ex.getMessage(),ex);
        return new Result(ex.getCode(), ex.getMessage());
    }


    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(AccessDeniedException.class)
    public Result accessDeniedExceptionHandler(HttpServletResponse response, AccessDeniedException ex) {
        log.error("异常信息【{}】",ex.getMessage(),ex);
        return new Result(ResultConstant.EX_UNAUTHORIZED, ex.getMessage());
    }

    @ResponseStatus
    @ExceptionHandler(BaseException.class)
    public Result baseExceptionHandler(HttpServletResponse response, BaseException ex) {
        log.error("异常信息【{}】",ex.getMessage(),ex);
        return new Result(ex.getCode(), ex.getMessage());
    }

    @ResponseStatus
    @ExceptionHandler(Exception.class)
    public Result otherExceptionHandler(HttpServletResponse response, Exception ex) {
        log.error("异常信息【{}】",ex.getMessage(),ex);
        return new Result(ResultConstant.EX_OTHER_CODE, ex.getMessage());
    }



}

